const http = require('http')

const server = http.createServer()

server.on('request', (req, res) => {
    res.setHeader("Access-Control-Allow-Origin", "*")
    if (req.url === '/api/test') {
        res.end(JSON.stringify('成功了'))
    } else {
        setTimeout(() => {
            res.end(JSON.stringify('错了'))
        }, 4000);
    }
})
const socketio = require("socket.io")(server, {
    cors: true
});
server.listen(1818)

socketio.on('connection', (socket) => {
    // 服务端connection和connect都行，客户端只有connect事件没有connection事件
    socket.emit('news', {
        hello: 'world'
    });
    socket.on('my other event', function (data) {
        console.log(data);
    });
})